//
//  AlertBgView.m
//  WolfKill
//
//  Created by 清正 on 2017/3/28.
//  Copyright © 2017年 Zhuge_Su. All rights reserved.
//

#import "AlertBgView.h"

@interface AlertBgView () <UIGestureRecognizerDelegate>

@end

@implementation AlertBgView

- (instancetype)initWithFrame:(CGRect)frame clickRemove:(BOOL)remove bgColor:(UIColor*)color {
    self =[super initWithFrame:frame];
    if (self) {
        self =[[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] firstObject];
        
        self.topView.backgroundColor =[UIColor clearColor];
        self.btmView.backgroundColor =[UIColor clearColor];
        self.centerView.backgroundColor =[UIColor clearColor];
        self.clickRemove =remove;
        self.backgroundColor =color;

        self.frame =CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame));
        UITapGestureRecognizer *top =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didBgView:)];
        top.delegate =self;
        [self addGestureRecognizer:top];
    }
    return self;
}

- (void)setBgColor:(UIColor *)bgColor {
    _bgColor =bgColor;
    self.backgroundColor =bgColor;
}

- (void)didBgView:(UITapGestureRecognizer *)sender {
    if (self.clickRemove) {
        [self removeView];
    }
}

- (void)showView {
    UIWindow *windiw =[[UIApplication sharedApplication]keyWindow];
    if (!windiw) {
        windiw =[[[UIApplication sharedApplication]windows] lastObject];
    }
    NSInteger count =windiw.subviews.count;
    [windiw insertSubview:self atIndex:count];
}

- (void)removeView {
    [self removeFromSuperview];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    //返回触摸点在视图中的当前坐标
    CGPoint point = [touch locationInView:self];
    
    NSArray *topSubView =self.topView.subviews;
    for (UIView *view in topSubView) {
        CGPoint point1 = [touch locationInView:view];
        BOOL isYES = NO;
        if (point1.x>0 && point1.x<CGRectGetWidth(view.frame) &&
            point1.y>0 && point1.y<CGRectGetHeight(view.frame)) {
            isYES =YES;
        }
        if (CGRectContainsPoint(self.topView.frame, point) && isYES) {
            return NO;
        }
    }
    
    NSArray *centerSubView =self.centerView.subviews;
    for (UIView *view in centerSubView) {
        CGPoint point1 = [touch locationInView:view];
        BOOL isYES = NO;
        if (point1.x>0 && point1.x<CGRectGetWidth(view.frame) &&
            point1.y>0 && point1.y<CGRectGetHeight(view.frame)) {
            isYES =YES;
        }
        if (CGRectContainsPoint(self.centerView.frame, point) && isYES) {
            return NO;
        }
    }
    
    NSArray *bottomSubView =self.btmView.subviews;
    for (UIView *view in bottomSubView) {
        CGPoint point1 = [touch locationInView:view];
        BOOL isYES = NO;
        if (point1.x>0 && point1.x<CGRectGetWidth(view.frame) &&
            point1.y>0 && point1.y<CGRectGetHeight(view.frame)) {
            isYES =YES;
        }
        if (CGRectContainsPoint(self.btmView.frame, point) && isYES) {
            return NO;
        }
    }
    [self endEditing:NO];
    return  YES;
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
